package com.hh.service.service.impl;

import com.hh.common.enums.IntegralTransactionType;
import com.hh.common.enums.NowMoneyTransactionType;
import com.hh.common.model.finance.UserRecharge;
import com.hh.common.model.user.User;
import com.hh.common.utils.DateUtil;
import com.hh.service.service.RechargePayService;
import com.hh.service.service.UserBillService;
import com.hh.service.service.UserRechargeService;
import com.hh.service.service.UserService;
import com.hh.service.service.user.UserAssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.math.BigDecimal;


/**
 * 支付类
 * +----------------------------------------------------------------------
 * | hh [ hh赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2022 https://www.hh.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed hh并不是自由软件，未经许可不能去掉hh相关版权
 * +----------------------------------------------------------------------
 * | Author: hh Team <admin@hh.com>
 * +----------------------------------------------------------------------
 */
@Service
public class RechargePayServiceImpl implements RechargePayService {

    @Autowired
    private UserRechargeService userRechargeService;

    @Autowired
    private UserService userService;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private UserBillService userBillService;

    @Autowired
    private UserAssetService userAssetService;

    /**
     * 支付成功处理
     * 增加余额，userBill记录
     * @param userRecharge 充值订单
     */
    @Override
    public Boolean paySuccess(UserRecharge userRecharge) {
        userRecharge.setPaid(true);
        userRecharge.setPayTime(DateUtil.nowDateTime());

        User user = userService.getById(userRecharge.getUid());

        BigDecimal payPrice = userRecharge.getPrice().add(userRecharge.getGivePrice());

        //赠送积分
        Integer giveIntegral = userRecharge.getGiveIntegral();

        Boolean execute = transactionTemplate.execute(e -> {
            // 订单变动
            userRechargeService.updateById(userRecharge);

            // 余额变动创建记录
            userAssetService.operationNowMoney(user.getUid(), payPrice, "add", NowMoneyTransactionType.RECHARGE.getCode(),userRecharge.getId());

            //赠送用户积分
            if(giveIntegral > 0){
                userAssetService.updateUserIntegralBalance(user.getUid(),giveIntegral,"add", IntegralTransactionType.RECHARGEGIFT.getCode(), 3,userRecharge.getOrderId());
            }

            //判断用户是否是vip
            if(!user.getIsVip()){
                user.setIsVip(true);
                user.setBecomeVipTime(DateUtil.nowDateTime());
                userService.updateById(user);
            }
            return Boolean.TRUE;
        });
        return execute;
    }
}
